﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;

namespace Directory.Web
{
    public partial class Admin_Controls_CategoryTree : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                IList<Directory.Model.CategoryInfo> categories = CategoryDataProxy.GetRootCategoriesForUser(HttpContext.Current.User.Identity.Name);

                if (categories == null)
                {
                    return;
                }

                foreach (Directory.Model.CategoryInfo cat in categories)
                {
                    TreeNode node = new TreeNode(cat.CategoryName, cat.CategoryId.ToString(), null, this.NavigateUrl + QueryKey + "=" + cat.CategoryId.ToString(), this.Target);
                    node.PopulateOnDemand = true;
                    TreeView1.Nodes.Add(node);
                }

                ExpandCurrentNode(TreeView1);
            }
        }

        #region Properties

        private string _userName;
        public string UserName
        {
            get { return _userName; }
            set { _userName = value; }
        }

        private string _target;
        public string Target
        {
            get { return _target; }
            set { _target = value; }
        }

        private string _navigateUrl;
        public string NavigateUrl
        {
            get { return _navigateUrl; }
            set { _navigateUrl = value; }
        }

        private string _queryKey = "categoryId";
        public string QueryKey
        {
            get { return _queryKey; }
            set { _queryKey = value; }
        }

        public int RootsCount
        {
            get { return this.TreeView1.Nodes.Count; }
        }

        #endregion

        protected void TreeView1_TreeNodePopulate(object sender, TreeNodeEventArgs e)
        {
            if (e.Node.ChildNodes.Count == 0)
            {
                IList<Directory.Model.CategoryInfo> childs = CategoryDataProxy.GetChildsOfCategory(int.Parse(e.Node.Value));
                foreach (Directory.Model.CategoryInfo cat in childs)
                {
                    TreeNode node = new TreeNode(cat.CategoryName, cat.CategoryId.ToString(), null, this.NavigateUrl + QueryKey + "=" + cat.CategoryId.ToString(), this.Target);
                    node.PopulateOnDemand = true;
                    e.Node.ChildNodes.Add(node);
                }
            }
        }

        private void ExpandCurrentNode(TreeView tv)
        {
            int categoryId;
            if (int.TryParse(Request.QueryString[QueryKey], out categoryId))
            {
                IList<Directory.Model.CategoryInfo> parentCateogries = CategoryDataProxy.GetRecursiveParentsOfCategory(categoryId);
                TreeNode baseNode = null;
                foreach (TreeNode node in tv.Nodes)
                {
                    if (node.Value == parentCateogries[0].CategoryId.ToString())
                    {
                        baseNode = node;
                        break;
                    }
                }
                for (int i = 1; i < parentCateogries.Count && baseNode != null; i++)
                {
                    baseNode = BoundChildNode(baseNode, parentCateogries[i]);
                }
                baseNode.Expanded = true;
                //baseNode.Selected = true;
            }
        }

        private TreeNode BoundChildNode(TreeNode node, Directory.Model.CategoryInfo cat)
        {
            node.PopulateOnDemand = true;
            node.Expand();
            foreach (TreeNode child in node.ChildNodes)
            {
                if (child.Value == cat.CategoryId.ToString())
                {
                    return child;
                }
            }
            return null;
        }
    }
}
